rd.scroller.contentItem = {};

function ContentItem(text, logo) {
    this.canvas = document.createElement("canvas");
    this.context = this.canvas.getContext("2d");
    this.text = text;
}
ContentItem.prototype.loadImage = function(callback) {
    var params = {},
	self = this;
    
    //Need to use makeRequest to get around cross-domain issues for loading SVG images.
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
    gadgets.io.makeRequest(this.logo, function(obj) {
	var data = obj.data;

	//Insert the SVG image into the DOM.
	self.svg = data;
	callback();
    }, params);
}
ContentItem.prototype.createCanvas = function(i) {
    var textCanvas = document.createElement("canvas"),	//Canvas to which the text is written so that the width of that text can be determined.
	textContext = textCanvas.getContext("2d"),
	svgCanvas = document.createElement("canvas"),	//Canvas on which the SVG image will be drawn.
	svgContext = svgCanvas.getContext("2d"),
	id = "svg" + i,
	width = 0;
    
    //Write the text to textCanvas.
    this.writeText(textContext);
    //scale = this.writeText(textContext);
    
    //Width = scaled text width + 10px padding + scaled image width
    width = textContext.measureText(this.text).width + 10 + svgCanvas.width;
    
    //width = textContext.measureText(this.text).width * scale + 10 + this.image.width * ratio;	//Regular image where font is scaled to fit vertically.
    //width = textContext.measureText(this.text).width * scale + 10 + svgCanvas.width;		//SVG image where font is scaled to fit vertically.
    
    this.context.canvas.width = width;
    this.context.canvas.height = scroller.height;
    
    //Write the text to the properly sized canvas.
    this.writeText(this.context);
    //scale = this.writeText(this.context);
    
    //Draw the image after the text and starting 5px from the top.
    this.context.drawImage(svgCanvas, 0, 0, svgCanvas.width, svgCanvas.height, textContext.measureText(this.text).width + 10, 5, svgCanvas.width, svgCanvas.height);
    
    //For regular and SVG images with scaled font.
    //this.context.drawImage(svgCanvas, 0, 0, svgCanvas.width, svgCanvas.height, textContext.measureText(this.text).width * scale + 10, 5, svgCanvas.width, svgCanvas.height);
    //this.context.drawImage(svgCanvas, 0, 0, svgCanvas.width, svgCanvas.height, textContext.measureText(this.text).width * scale + 10, 0, this.image.width * ratio, this.image.height * ratio);
    
    //Remove the canvas containing the SVG image since we're finished with it.
    document.body.removeChild(svgCanvas);
    
    return this.canvas;
}
ContentItem.prototype.writeText = function(context) {
    var topOffset = context.canvas.height / 2,	//Y coordinate at which to being drawing (vertical alignment).
	rules, canvasFont;
	//fontHeight = this.getFontHeight("font: " + canvasFont + ";");
	//scale = 0;
	
    rules = this.parseCSSRule(document.styleSheets[1].cssRules[0].cssText);
    canvasFont = rules[2] + rules[3] + rules[0];

    context.font = canvasFont;
    context.strokeStyle = rules[1];
    context.textAlign = 'left';
    context.textBaseline = 'middle';
    
    context.save();
    context.translate(0, topOffset);
    
    //Scale the font to fill the scroller's height.
    //scale = scroller.height / fontHeight;
    //context.scale(scale, scale);
    
    context.fillStyle = rules[1];
    context.fillText(this.text, 0, 0);			//Text
    context.strokeText(this.text, 0, 0);		//Text outline
    
    context.restore();
    
    //return scale;
}
ContentItem.prototype.getFontHeight = function(fontStyle) {
    var body = document.getElementsByTagName("body")[0],
	dummy = document.createElement("div"),
	dummyText = document.createTextNode("M"),
	result;
    
    dummy.appendChild(dummyText);
    dummy.setAttribute("style", fontStyle);
    body.appendChild(dummy);
    result = dummy.offsetHeight;
    body.removeChild(dummy);
    
    return result;
}
//Find a better way to parse this and make this code common.
ContentItem.prototype.parseCSSRule = function(s) {
    var a = s.indexOf('{');
    var b = s.indexOf('}');
    var selector = s.substring(0, a);
    var rules = s.substring(++a, b).split(';');
    var values = [];
    
    //Now remove property name and just keep the value.
    for (var i = 0; i < rules.length; i++) {
	values.push(rules[i].substring(rules[i].indexOf(":", 0) + 1));
    }

    return values;
  }